package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.ClassroomDao;
import com.swt.domain.Classroom;
import com.swt.domain.Shop;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
@Repository(value = "TClassroomDao")
public class ClassroomDaoImp  implements ClassroomDao {
    @Resource
    private HibernateTemplate hibernateTemplate;

    @Override
    public List<Classroom> classromQuery(int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Classroom where clubClubId = ? ";
        List<Classroom> coursesList = session.createQuery(sql).setParameter(0,clubClubId).list();
        return coursesList;
    }
    //添加教室
    @Override
    public boolean classromAdd(String classroomName, String classroomCapacity, int shopid,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Shop shop = session.load(Shop.class,shopid);
        Classroom classroom = new Classroom();
        classroom.setShop(shop);
        classroom.setClassroomName(classroomName);
        classroom.setClassroomCapacity(classroomCapacity);
        classroom.setClubClubId(clubClubId);
        try {
            session.save(classroom);
        }catch (Exception e){
            return false;
        }

        return true;
    }

    //查询使用原生sql查询
    @Override
    public List<Classroom> classromQueryDao(int id) {

        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createSQLQuery("select * from classroom where classroom_shop_id = ?");
        List<Classroom> classroomList= query.setParameter(0,id).list();

        return classroomList;
    }
//删除教室
    @Override
    public boolean classDeleteDao(int classid) {

        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Classroom classroom = session.get(Classroom.class,classid);
        try {
            session.delete(classroom);
        }catch (Exception e){
            return false;
        }
        return true;


    }
}
